/*
 * This file is part of GuitarMaster.
 * 
 * GuitarMaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GuitarMaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with GuitarMaster.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.paris5.master2.guitarmaster.service;

import com.paris5.master2.guitarmaster.bean.EcomCategory;
import com.paris5.master2.guitarmaster.bean.EcomCountry;
import com.paris5.master2.guitarmaster.bean.EcomStore;
import com.paris5.master2.guitarmaster.dao.EcomDao;
import com.paris5.master2.guitarmaster.entity.Category;
import com.paris5.master2.guitarmaster.entity.Country;
import com.paris5.master2.guitarmaster.entity.Store;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/*
 * $Id: EcomDaoService.java 7 2013-03-13 08:38:30Z nedjahi.mourad@gmail.com $
 * 
 * $HeadURL: https://com-paris5-master2-guitarmaster-ejb.googlecode.com/svn/trunk/com.paris5.master2.guitarmaster-ejb/src/java/com/paris5/master2/guitarmaster/dao/EcomDaoService.java $
 * $Revision: 7 $
 * $Date: 2013-03-13 09:38:30 +0100 (mer., 13 mars 2013) $
 * $Author: nedjahi.mourad@gmail.com $
 */
@Stateless(mappedName="list")
public class ServiceList implements ServiceListRemote {
    
    @EJB private EcomDao dao; 
    private int nbCoutriesMax=1000;

    @Override
    public List<EcomCategory> getCategories(int offset, int limit) {
        ArrayList<EcomCategory> res = null;
        List<Object> result = dao.findAll(Category.class, offset, limit);
        if(!result.isEmpty()){
            res = new ArrayList();
            for(Object ep : result){
                res.add(((Category) ep).getEcomCategory());
            }
        }
        return res;
    }

    @Override
    public List<EcomCountry> getCountries(int offset, int limit) {
        ArrayList<EcomCountry> res = null;
        List<Object> result = dao.findAll(Country.class, offset, limit);
        if(!result.isEmpty()){
            res = new ArrayList();
            for(Object ep : result){
                res.add(((Country) ep).getEcomCountry());
            }
        }
        return res;
    }

    @Override
    public List<EcomStore> getStores(int offset, int limit) {
        ArrayList<EcomStore> res = null;
        List<Object> result = dao.findAll(Store.class, offset, limit);
        if(!result.isEmpty()){
            res = new ArrayList();
            for(Object ep : result){
                res.add(((Store) ep).getEcomStore());
            }
        }
        return res;
    }
    
    @Override
    public List<String> getCountriesCodes(){
        ArrayList<String> res = null;
        List<Object> result = dao.findAll(Country.class, 0, nbCoutriesMax);
        if(result!=null){
            res = new ArrayList();
            for(Object obj: result){
                Country country = (Country) obj;
                res.add(country.getCode());
            }
        }
        
        return res;
    }

    @Override
    public Integer countCategories() {
        return dao.countAll(Category.class);
    }

    @Override
    public Integer countCountries() {
        return dao.countAll(Country.class);
    }

    @Override
    public Integer countStores() {
        return dao.countAll(Store.class);
    }

    @Override
    public List<String> getCountriesDevise() {
        List<EcomCountry> contries=getCountries(0, nbCoutriesMax);
        List<String> devises=null;
        for(EcomCountry contry:contries){
            if(! this.contains(devises, contry.getDevise())) {
                devises.add(contry.getDevise());
            }
        }
        return devises;
        
    }
    
    private boolean contains(List<String> devises, String devise){
        if(devises==null ||devises.isEmpty()){
            return false;
        }
        Iterator<String> it=devises.iterator();
        boolean exist=false;
        while(it.hasNext()&& !exist){
            if(it.next().equals(devise)){
                exist=true;
            }
        }
        return true;
    }
}
